<?php
namespace cms\modules\backend\controllers;

use Yii;
use cms\controllers\AuthController;
use app\models\ContentFlag;
use app\models\Content;
use app\models\ContentSearch;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;
use yii\db\Expression;


class ContentFlagController extends AuthController
{
    private $flag = ['a', 'b', 'c', 'd', 'e'];

    /*内容标示列表*/
    public function actionContentFlagList()
    {
        $contentFlag = new ContentFlag;
        $contentFlagList = $contentFlag::find()
                            ->select(['id', 'flag', 'flagname'])
                            ->where('app_id = :app_id', [':app_id' => $this->app_id])
                            ->asArray()->all();

        return Tool::showRes([0, '获取成功'], $contentFlagList);
    }

    /*添加内容标示*/
    public function actionAddContentFlag()
    {
        if(Yii::$app->request->isPost){
            $contentFlag = new ContentFlag;
            $contentFlagList = $contentFlag::find()->select(['flag'])
                ->where('app_id = :app_id', [':app_id' => $this->app_id])
                ->asArray()->all();
            // P($this->getFlag($contentFlagList));
            if(count($contentFlagList) < count($this->flag)){
                $this->post['ContentFlag']['flag'] = $this->getFlag($contentFlagList);
                if($contentFlag->addFlag($this->post, $this->app_id)){
                    $backInfo = [0, '添加APP内容标示成功'];//返回信息
                }else{
                    if($contentFlag->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $contentFlag->getErrors();//异常信息
                    }
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*修改内容标示的名称*/
    public function actionModifyContentFlag()
    {
        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['ContentFlag']['id'])?intval($this->post['ContentFlag']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $contentFlag = new ContentFlag;
                if($contentFlag->modifyFlag($this->post, $id)){
                    $backInfo = [0, '修改内容标示成功'];//返回信息
                }else{
                    if($contentFlag->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $contentFlag->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除内容标示*/
    public function actionDeleteContentFlag()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $contentFlag = ContentFlag::find()->where('id = :id', [':id' => $id])->one();
            if(!is_null($contentFlag)){
                if($contentFlag->sys == 2){
                    $backInfo = ErrMap::getError(10013, '系统参数不能删除');//返回信息
                }else{
                    if($contentFlag->delete()){
                        $backInfo = [0, '删除内容属性成功'];//返回信息
                    }else{
                        $backInfo = ErrMap::getError(10013, '删除内容属性失败');//返回信息
                    }
                }
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));

    }


    /*选出一个 flag*/
    private function getFlag($contentFlagList)
    {
        $res = '';
        foreach($this->flag as $k => $v){
            $has = false;
            foreach($contentFlagList as $k1 => $v1){
                if($v == $v1['flag']){
                    $has = true;
                    break;
                }
            }
            if(!$has){
                $res = $v;
                break;
            }
        }
        return $res;

    }


    /*取出content表中带有此属性的内容*/
    public function actionFlagContentList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();
        $this->get['app_id'] = $this->app_id;
        $this->get['state'] = 3;
        $this->get['display'] = 2;
        // P($this->get);

        $lang = isset($this->get['lang'])?$this->get['lang']:'zh';

        $id = isset($this->get['id'])?intval($this->get['id']):'';
        if($id){
            $contentFlag = ContentFlag::find()->select(['flag', 'zhsorts', 'ensorts', 'jpsorts', 'krsorts'])->where('id = :id', [':id' => $id])->one();
            // P($contentFlag);
            if(!is_null($contentFlag)){
                $flag = $contentFlag['flag'];
            }
            $search = (new ContentSearch())->search($this->get);
            if($search === false){//验证搜索参数不通过
                $count = 0;
                $contentList = array();
            }else{
                $count = $search->andWhere(new Expression("FIND_IN_SET(:flag, flag)", [":flag"=>$flag]))->count();
                $contentList = $search->select(['content_id', 'title', 'flag', 'pc_preview_count', 'mobile_preview_count', 'add_time'])
                              ->andWhere(new Expression("FIND_IN_SET(:flag, flag)", [":flag"=>$flag]))
                              ->offset($pageSize*($currPage-1))->limit($pageSize)
                              ->asArray()->all();
                /*按照排序显示*/
                $langsorts = $lang . 'sorts';
                // P($contentFlag[$langsorts]);
                if(!empty($contentFlag[$langsorts])){
                    $sorts = explode(',', $contentFlag[$langsorts]);
                    $sortList = [];
                    foreach($sorts as $k => $v){
                        foreach($contentList as $k1 => $v1){
                            if($contentList[$k1]['content_id'] == $v){
                                array_push($sortList, $contentList[$k1]);
                                unset($contentList[$k1]);
                            }
                        }
                    }
                    // P($sortList);
                    $contentList = $sortList;
                }

            }
        }else{
            $count = 0;
            $contentList = [];
        }
        // P($contentList);
        $res = array(
            'list' => $contentList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }


    /*排序*/
    public function actionContentFlagSort()
    {
        $id = isset($this->post['id'])?intval($this->post['id']):'';
        $lang = isset($this->post['lang'])?$this->post['lang']:'zh';
        $langsorts = $lang . 'sorts';

        $this->post['ContentFlag'][$langsorts] = isset($this->post['sort'])?implode(',', $this->post['sort']):'';
        // P($this->post);
        if(!$id){
            $backInfo = ErrMap::getError(10005);//返回信息
        }else{
            $contentFlag = new ContentFlag;
            if($contentFlag->modifyFlag($this->post, $id)){
                $backInfo = [0, '修改属性内容排序成功'];//返回信息
            }else{
                if($contentFlag->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $contentFlag->getErrors();//异常信息
                }
            }
        }

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

}
